package com.gopay.cashier.web.security;

import com.gopay.cashier.service.security.GopayDtCertService;
import com.gopay.cashier.web.controller.BaseController;
import com.gopay.common.security.vo.UserDtCertInfoVO;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 国付宝动态令牌和数字证书Controller
 *
 * @GopayDtCertController.java
 * @author fanghw
 * @2016年9月22日 下午2:23:07 www.gopay.com.cn Inc.All rights reserved.
 */
@Controller
@RequestMapping("/security/gopaydtcert/")
public class GopayDtCertController extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(GopayDtCertController.class);

    @Resource(name = "gopayDtCertService")
    private GopayDtCertService gopayDtCertService;


    /**
     * 获取开关和证书序列号
     * @return
     */
    @RequestMapping(value = "/getOnOffAndCertSN")
    @ResponseBody
    public String getOnOffAndCertSN() {
        String strJson = null;

        try {
            String userId=getLoginUser().getUser().getUserId();
            //userId="131331";
            UserDtCertInfoVO retVo =gopayDtCertService.getOnOffAndCertSN(userId);

            // 生成json字符串
            strJson = JSONObject.fromObject(retVo).toString();

        } catch (Exception e) {
            // 处理异常信息
            logger.error("查询异常", e);
        }

        return strJson;

    }

    /**
     * 获取证书UUID
     * @return
     */
    @RequestMapping(value = "/getUUID")
    @ResponseBody
    public String getUUID(HttpServletRequest request) {
        String strJson = null;

        try {

            String uuid= gopayDtCertService.getUUID(request);

            Map<String,String> retMap=new HashMap<String,String>();
            retMap.put("uuid", uuid);

            // 生成json字符串
            strJson = JSONObject.fromObject(retMap).toString();

        } catch (Exception e) {
            // 处理异常信息
            logger.error("查询异常", e);
        }

        return strJson;

    }

}
